package com.newtype.meeting.client.widget.frontend;

import java.util.HashMap;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Meeting;
/**
 * 修改会议公开对象窗口
 * @author 汪恭泽
 *
 */
public class EditOpenTypeWindow extends Window {
	private HiddenField<String> openID;//公开ID
	private TextField<String> openName;//公开对象name
	private RadioGroup openType;

	public EditOpenTypeWindow(Meeting meeting) {
		setSize(500, 280);
		setModal(true);

		ContentPanel cp = new ContentPanel();
		cp.setHeaderVisible(false);
		cp.setStyleAttribute("padding", "5px");
		cp.setFrame(true);

		StringBuffer sb = new StringBuffer();
		sb.append("<div style='line-height: 2em;'>");
		sb
				.append("<p style='font-size:18px;font-weight: bold;color:#0060A0;'>修改公开对象</p>");
		sb.append("<hr size='1'>");
		sb.append("</div>");
		Text title = new Text(sb.toString());
		title.setStyleAttribute("background", "white");
		title.setStyleAttribute("padding", "8px");
		cp.add(title);

		FormPanel formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setStyleAttribute("background", "white");
		formPanel.setButtonAlign(HorizontalAlignment.CENTER);

		openID = new HiddenField<String>();
		openID.setFieldLabel("公开对象ID");
		openID.setName("openID");
		openID.setValue(meeting.getOpenID());
		formPanel.add(openID);
		
		openName = new TextField<String>();
		openName.setFieldLabel("<b>请选择对象</b>");
		openName.setName("openName");
		openName.setValue(meeting.getOpenName());
		openName.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				Map<String, Field<String>> organization = new HashMap<String, Field<String>>();
				organization.put("IDStr", openID);
				organization.put("nameStr", openName);
				Dispatcher.forwardEvent(AppEvents.ToOrganizationSeperateWindow,
						organization);
			}
		});
		openName.hide();

		Radio radio1 = new Radio();
		radio1.setBoxLabel("全公司");
		radio1.setValueAttribute("0");
		radio1.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				openName.hide();
			}
		});

		Radio radio2 = new Radio();
		radio2.setBoxLabel("自订对象");
		radio2.setValueAttribute("2");
		radio2.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				openName.show();
			}
		});

		openType = new RadioGroup();
		openType.setFieldLabel("<b>会议公开层级</b>");
		openType.setName("openType");
		openType.setAutoWidth(true);
		openType.setStyleAttribute("background", "#FFFFC2");
		openType.add(radio1);
		openType.add(radio2);
		//判断会议公开对象(2:自订对象，0:全公司)
		if (meeting.getOpenType() == "2" || meeting.getOpenType().equals("2")) {
			openType.setValue(radio2);
			openName.show();
		} else {
			openType.setValue(radio1);
		}
		formPanel.add(openType);
		formPanel.add(openName, new FormData("100%"));
		formPanel.addButton(getSaveButton(meeting));

		cp.add(formPanel);
		add(cp);
	}

	private Button getSaveButton(final Meeting meeting) {
		Button button = new Button("送出");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				String typeValue = openType.getValue().getValueAttribute();
				if (typeValue == "2" || typeValue.equals("2")) {
					meeting.setOpenID(openID.getValue());
					meeting.setOpenName(openName.getValue());
				} else {
					meeting.setOpenID("");
					meeting.setOpenName("");
				}
				meeting.setOpenType(typeValue);
				Dispatcher.forwardEvent(AppEvents.UpdateOpenType, meeting);
				hide();
			}
		});
		return button;
	}
}
